// TODO: 风扇
#include "bsp_gpioDev.h"

#include <stdio.h>

#include "lz_hardware.h"


struct GpioDev
{
    int id;
    int gpio_pin;
    // int drive_level;
    int state;
};

gpioDev_t fan_masterBedroom = {
    .id = 0,
    .gpio_pin = GPIO0_PB0,
    .state = 0
},
fan_kitchen = {
    .id = 1,
    .gpio_pin = GPIO0_PB1,
    .state = 0
},
curtain_livingRoom = {
    .id = 2,
    .gpio_pin = GPIO0_PB2,
    .state = 0
},
humidifier_masterBedroom = {
    .id = 3,
    .gpio_pin = GPIO0_PB3,
    .state = 0
};

int gpioDev_init(gpioDev_t* self)
{
    if(NULL == self)
    {
        return -1;
    }

    LzGpioInit(self->gpio_pin);
    PinctrlSet(self->gpio_pin, MUX_FUNC0, PULL_DOWN, DRIVE_LEVEL0);
    LzGpioSetDir(self->gpio_pin, LZGPIO_DIR_OUT);
    LzGpioSetVal(self->gpio_pin, LZGPIO_LEVEL_LOW);

    return 0;
}

int gpioDev_set_state(gpioDev_t* self, int state)
{
    if(NULL == self)
        return -1;

    if(state)
    {
        LzGpioSetVal(self->gpio_pin, LZGPIO_LEVEL_HIGH);
        self->state = 1;
    }
    else
    {
        LzGpioSetVal(self->gpio_pin, LZGPIO_LEVEL_LOW);
        self->state = 0;
    }

    printf("[gpioDev_set_state] set state: %d.\n", state);

    return 0;
}

int gpioDev_get_state(gpioDev_t* self, int* pState)
{
    if(NULL == self || NULL == pState)
        return -1;
    
    *pState = self->state;

    return 0;
}

int gpioDev_init_all(void)
{
    gpioDev_init(&fan_masterBedroom);
    gpioDev_init(&fan_kitchen);
    gpioDev_init(&curtain_livingRoom);
    gpioDev_init(&humidifier_masterBedroom);
    //...

    return 0;
}
